<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
    var page_type = 'extract';  //设置页面名称
    var queryParams ;
    var tables = $('#dataTables').DataTable({
		"sAjaxSource" : '{{ urlfor "RetailController.DataGrid"}}'
		,"bLengthChange": false
		,"fnServerData" : function(sSource, aoData, fnCallback) {
            queryParams =checkQueryTableData(aoData);
            var form = $('#search-form').serializeArray();
            $.each(form, function(i, value){
                queryParams[value.name] = value.value;
            });
			$.post(sSource, JSON.stringify(queryParams),function(resp) {
				fnCallback(resp);
			},'json')
		}
		,columns: [
			{"bSortable": false, render:function(data, type, full, meta) {
			    var d = [];
			    d.push('<input type="hidden" class="sales_status" data-value="'+  full.IsPayment +'">');
			    d.push('<div class="am-text-left">');
			    d.push('<div class="am-margin-bottom-xs">'+ full.ManualNumber +'</div>');
                d.push('<div>销售员：'+ full.BusinessName +'</div>');
                d.push('<div>'+ timetrans(full.CreatedAt) +'</div>');
			    d.push('</div>');
				return d.join('');
			}}
            ,{"bSortable": false, render:function(data, type, full, meta) {
                    var d = [];
                    d.push('<div class="am-text-left">');
                    d.push('<div>'+ full.Archives.Realname +'</div>');
                    d.push('<div>'+ full.Archives.Mobile +'</div>');
                    d.push('</div>');
                    return d.join('');
                }}
            ,{"bSortable": false, render:function(data, type, full, meta) {
                    var d = [];
                    d.push('<div class="am-text-left">');
                    d.push('<div class="am-text-sm">￥'+ full.SumPayable +'</div>');
                    d.push('<div>已付款：￥'+ full.AlreadyMoney + '<span class="am-text-danger am-margin-left">待付款：￥' +  (full.SumPayable-full.AlreadyMoney).toFixed(2) +'</span></div>');
                    d.push('</div>');
                    return d.join('');
                }}
            ,{"bSortable": false, render:function(data, type, full, meta) {
                    var d = [];
                    d.push('<div class="am-text-left">');
                    var uptype_name = '';
                    if(full.PickUpType == 1) {
                        uptype_name = '现场取货';
                    }else if(full.PickUpType == 2) {
                        uptype_name = '邮递取货';
                    }else if(full.PickUpType == 3) {
                        uptype_name = '指定日期取货';
                    }
                    switch (full.IsPayment) {
                        case 1:
                            d.push('<div class="am-text-sm am-text-primary">待付款 - '+ uptype_name +'</div>');
                            break;
                        case 2:
                            d.push('<div class="am-text-sm am-text-secondary">待取货 - '+ uptype_name +'</div>');
                            break;
                        case 3:
                            d.push('<div class="am-text-sm am-text-warning">部分退货 - '+ uptype_name +'</div>');
                            break;
                        case 4:
                            d.push('<div class="am-text-sm am-text-danger">已退货 - '+ uptype_name +'</div>');
                            break;
                        case 5:
                            d.push('<div class="am-text-sm am-text-success">已完成 - '+ uptype_name +'</div>');
                            break;
                        case 6:
                            d.push('<div class="am-text-sm">已作废</div>');
                            break;
                    }
                    if(full.HopePickUpTime && full.PickUpType == 3) {
                        d.push('<div>'+timetransDate(full.HopePickUpTime)+'</div>');
                    }
                    d.push('</div>');
                    return d.join('');
                }}
			,{   
				"mDataProp": "Action", 
				"bSortable": false, 
				"sWidth":"10%",
				"render": function ( data, type, full, meta ) {
                    var d = [];
                    switch (full.IsPayment) {
                        case 1:
                            d.push('<a href="javascript:;" data-retid="'+ full.Id +'" data-pickuptype="'+ full.PickUpType +'" onclick="return popWind(this);">付款结算</a>');
                            d.push('&nbsp;&nbsp;<a href="javascript:;" data-retid="'+ full.Id +'" onclick="return discard(this);" class="am-text-danger">作废</a>');
                            break;
                        case 2:
                            d.push('<a href="javascript:;"" data-retid="'+ full.Id +'" data-pickuptype="'+ full.PickUpType +'" onclick="return popWind(this);">取镜结算</a>');
                            break;
                        case 5:
                            d.push('<a href="javascript:;"" data-retid="'+ full.Id +'" data-pickuptype="'+ full.PickUpType +'" onclick="return popWindView(this);">查看详情</a>');
                            break;
                        case 3:
                            d.push('<a href="javascript:;"" data-retid="'+ full.Id +'" data-pickuptype="'+ full.PickUpType +'" onclick="return popWindView(this);">查看详情</a>');
                            break;
                        case 4:
                            d.push('<a href="javascript:;"" data-retid="'+ full.Id +'" data-pickuptype="'+ full.PickUpType +'" onclick="return popWindView(this);">查看详情</a>');
                        case 6:
                            d.push('<a href="javascript:;"" data-retid="'+ full.Id +'" data-pickuptype="'+ full.PickUpType +'" onclick="return popWindView(this);">查看详情</a>');
                            break;
                    }
                    return d.join('');
				}  
			 } 
		]
		,"aLengthMenu":[20, 50, 100, 200]
		,'bLengthChange': true
		,"serverSide": true
		,"bPaginate" : true
		,"bFilter" : false	//是否启动过滤、搜索功能 
		,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
		,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示  
        ,"drawCallback": function ( settings ) {
            $('#dataTables').find('.sales_status[data-value="6"]').parent().parent().css({'color':'red'});
        }
	});
	$('#query-submit').click(function(){
        queryParams.offset = 0;
        reloadTable();
    });
	function reloadTable() {
		$("#dataTables").dataTable().fnDraw(false);
	}
	function dialogReback() {
		reloadTable();
	}
	$('#refresh').click(function(){
		reloadTable();
		return false;
	});

	function popWind(obj) {
        var o = $(obj);
        var sales_id = o.data('retid');
        var pickuptype = o.data('pickuptype');

        if(pickuptype==1||pickuptype==3){
            layer.open({
                type: 2
                , title: '取镜结算'
                , shadeClose: true
                , shade: 0.2
                , id: 'search_archives_dialog'
                , area: ['100%', "100%"]
                , content: ['{{ urlfor "RetailController.PickupPayConfirm" }}' + '?sales_id='+ sales_id]
                , anim: 1 //动画类型
                ,cancel: function(index, layero){
                    layer.close(index);
                    return false;
                }
            });
        }else{
            layer.open({
                type: 2
                , title: '发货结算'
                , shadeClose: true
                , shade: 0.2
                , id: 'search_archives_dialog'
                , area: ['100%', "100%"]
                , content: ['{{ urlfor "RetailController.SendoutPickupPayConfirm" }}' + '?sales_id='+ sales_id]
                , anim: 1 //动画类型
                ,cancel: function(index, layero){
                    layer.close(index);
                    return false;
                }
            });
        }
    }
    function popWindView(obj) {
	    var o = $(obj);
        var sales_id = o.data('retid');
        layer.open({
            type: 2
            , title: '单据详情'
            , shadeClose: true
            , shade: 0.2
            , id: 'search_retail_view_dialog'
            , area: ['100%', "100%"]
            , content: ['{{ urlfor "RetailController.View" }}' + '?sales_id='+ sales_id]
            , anim: 1 //动画类型
            ,cancel: function(index, layero){
                layer.close(index);
                return false;
            }
        });
    }
    function discard(obj) {
        var o = $(obj);
        var sales_id = o.data('retid');
        layer.confirm("确定要作废该单据吗？",function (index) {
            layer.close(index);
            $.post('{{ urlfor "RetailController.Discard" }}',{'sales_id':sales_id},function (res) {
                if(res.code == 1) {
                    layer.msg(res.msg);return false;
                }
                $('#query-submit').click();
            });
        })
    }

</script>